Introduction

We are going to reproduce three different plots, of which we are only given the data set and the final image file. Each of them has two versions:

  • The first version is a plot made just using the layer functions of ggplot2 and leaving everything else with the defaults. This is, using ggplot, geom_*, stat_* and annotate_* functions (click on the image to see it bigger).

random image random image random image

  • The second version builds from the first plot to create a more complex display with customized scales_*, coord_*, facet_* or theme_*. Helper functions such as labs() or guides() could also be used (click on the image to see it bigger).

random image random image random image

library(ggplot2)

: ggplot2 will automatically load all three data sets used in this practical.

Organization of the practical

You will see different icons through the document, the meaning of which is:

: additional or useful information
: a worked example
: a practical exercise
: a space to answer the exercise
: a hint to solve an exercise
: a more challenging exercise


Simple versions

Plot 1

# Dataset used: diamonds
 
ggplot(data = diamonds, mapping = aes(x = clarity, y = log10(carat))) +
  geom_jitter(aes(colour = cut)) +
  geom_violin(fill = "black") 

Plot 2

# Dataset used: mpg

ggplot(data = mpg, mapping = aes(x = manufacturer, fill = class)) +
  geom_bar() + 
  geom_text(aes(label=after_stat(count)), stat='count', position=position_stack(vjust=0.5))

Need help with the count numbers shown in each stacked bar? https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2

Plot 3

# Dataset: ToothGrowth

ggplot(data = ToothGrowth, mapping = aes(x = factor(dose), y = len, fill = supp)) +
  geom_boxplot() + 
  geom_hline(yintercept = mean(ToothGrowth$len), alpha = 0.5, linetype="dashed") + 
  annotate("text", x = 1, y = 20, label = 'mean')


Complex versions

Plot 1

ggplot(data = diamonds, mapping = aes(x = clarity, y = log10(carat))) +
  geom_jitter(aes(colour = cut), show.legend = FALSE) +
  geom_violin(fill = "black")  + 
  facet_grid(~factor(cut)) + 
  labs(title = "Diamond quality")

Plot 2

ggplot(data = mpg, mapping = aes(y = manufacturer, fill = class)) +
  geom_bar() + 
  geom_text(aes(label=after_stat(count)), stat='count', position=position_stack(vjust=0.5)) + 
  labs(x = "Number of cars") + 
  scale_fill_brewer(palette="Dark2", name  = "Car type")

This graph uses a scale_fill_brewer palette, can you find which one? Dark2

Plot 3

ggplot(data = ToothGrowth, mapping = aes(x = factor(dose), y = len, fill = supp)) +
  geom_boxplot() + 
  geom_hline(yintercept = mean(ToothGrowth$len), alpha = 0.5, linetype="dashed") + 
  annotate("text", x = 1, y = 20, label = 'mean') + 
  scale_fill_brewer(palette="Oranges", name  = "Supplement", direction = -1, labels=c("Orange juice", "Vitamin C")) +
  theme_classic() +
  labs(x = "Dose (mg/day)", y = "Tooth length", caption = "Source: ToothGrowth dataset")

This graph uses a scale_fill_brewer palette, can you find which one? Oranges